package cpmake.addons.ivy;

//Do not import ivy classes here.
import cpmake.CPMake;
import cpmake.CPMakeException;
import cpmake.util.Validation;

import java.io.File;
import java.util.*;

public class IvyAddon
	{
	private boolean m_isSetup = false;
	
	private File m_ivyFile;
	private ResolveRule m_resolveRule;
	private RetrieveRule m_retrieveRule;
	private File m_settingsFile = null;
	private Map<Set<String>, ResolveRule> m_resolveRules =
			new HashMap<Set<String>, ResolveRule>();

	public IvyAddon()
			throws CPMakeException
		{
		CPMake make = CPMake.getCurrentMake();
		m_ivyFile = make.file("ivy.xml");
		
		ClassLoader cl = getClass().getClassLoader();
		if (cl.getResource("org/apache/ivy/Ivy.class") == null)
			throw new CPMakeException("Please place the Ivy jar file in the classpath or in the same directory as the cpmake jar");
		}
		
	public IvyAddon setSettingsFile(String settingsFile)
			throws CPMakeException
		{
		m_settingsFile = Validation.fileMustExist(settingsFile);

		return (this);
		}
		
	public IvyAddon setup()
		{
		//check if m_ivyFile exists else throw error
		/* 
		Create rule for 
		Publish
		*/

		//I think I want to remove these
		Set<String> conf = Collections.singleton("*");
		m_resolveRule = new ResolveRule(m_ivyFile, m_settingsFile, Collections.singleton("*"));
		m_resolveRule.setName("ivy-resolve");
		m_resolveRules.put(conf, m_resolveRule);

		m_retrieveRule = new RetrieveRule(m_ivyFile);
		m_retrieveRule.setResolveRule(m_resolveRule);
		
		m_isSetup = true;
		return (this);
		}
		
	//---------------------------------------------------------------------------
	private void ensureSetup()
			throws CPMakeException
		{
		if (!m_isSetup)
			throw new CPMakeException("You must call setup first", -1);
		}
		
	//---------------------------------------------------------------------------
	public ResolveRule getResolveRule(String configuration)
			throws CPMakeException
		{
		return (getResolveRule(Collections.singleton(configuration)));
		}

	//---------------------------------------------------------------------------
	public ResolveRule getResolveRule(Object... configurations)
			throws CPMakeException
		{
		if ((configurations.length == 1) && (configurations[0] instanceof Iterable))
			return (getResolveRule((Iterable<Object>) configurations[0]));
		else
			return (getResolveRule(Arrays.asList(configurations)));
		}

	//---------------------------------------------------------------------------
	public ResolveRule getResolveRule(Iterable<Object> configurations)
			throws CPMakeException
		{
		ensureSetup();

		Set<String> configSet = new HashSet<String>();
		for (Object o : configurations)
			configSet.add(o.toString());

		ResolveRule rule = m_resolveRules.get(configSet);
		if (rule == null)
			{
			rule = new ResolveRule(m_ivyFile, m_settingsFile, configSet);
			m_resolveRules.put(configSet, rule);
			}

		return (rule);
		}

	//---------------------------------------------------------------------------
	public RetrieveRule getRetrieveRule(String configuration)
			throws CPMakeException
		{
		ensureSetup();
		return (m_retrieveRule);
		}
	}
